// src/stores/GptStore.ts
import { defineStore } from 'pinia'

// 定义分析数据的接口
interface SpotAnalysisResponse {
    status: string
    message: string | null
    data: {
        spot_details: Array<{
            name: string
            description: string
            detailed_analysis: string
            visiting_tips: string
            budget_estimation: string
            recommended_duration: string
        }>
        analysis: {
            summary: string
            travel_tips: string
            budget_analysis: string
        }
    }
}

// 定义 Store 的状态接口
interface GptStoreState {
    analysisHistory: Array<{
        timestamp: string
        data: SpotAnalysisResponse
    }>
    lastAnalysis: SpotAnalysisResponse | null
    loading: boolean
}

// 使用类型定义的 Store
export const useGptStore = defineStore('gpt', {
    state: (): GptStoreState => ({
        analysisHistory: [],
        lastAnalysis: null,
        loading: false
    }),

    actions: {
        async addAnalysis(analysisData: SpotAnalysisResponse) {
            try {
                const newEntry = {
                    timestamp: new Date().toISOString(),
                    data: analysisData
                }
                this.analysisHistory.unshift(newEntry)
                if (this.analysisHistory.length > 10) {
                    this.analysisHistory = this.analysisHistory.slice(0, 10)
                }
                this.lastAnalysis = analysisData
            } catch (error) {
                console.error('保存分析结果失败:', error)
                throw error
            }
        }
    }
})

// 导出类型
export type { SpotAnalysisResponse, GptStoreState }